package com.adams.action.icon;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import com.adams.action.view.ViewAction;
import com.adams.custom.page.util.ViewUtil;
import com.adams.info.AdamsConstant;
import com.adams.utils.FileUtil;
import com.adams.utils.SystemConfig;

/**
 * File Name : IconAction.java
 * 
 * @Description : icon action类
 * 
 * @author 904176
 * 
 */
public class IconAction extends ViewAction
{

	private static final long serialVersionUID = 1L;
	private File url; // icon
	private String urlFileName;
	private String urlContentType;
	// 标题图物理路径
	private final String iconDir = (String) SystemConfig.getInstance()
			.getProperty("icon.iconDir");
	// 标题图虚拟路径
	private final String iconUrl = (String) SystemConfig.getInstance()
			.getProperty("icon.iconUrl");
	// 图标最大值
	private final int maxSize = Integer.parseInt((String) SystemConfig
			.getInstance().getProperty("icon.size"));
	// 可以上传的图片类型
	private final String iconType = (String) SystemConfig.getInstance()
			.getProperty("icon.type");

	public File getUrl()
	{
		return url;
	}

	public void setUrl(File url)
	{
		this.url = url;
	}

	public String getUrlFileName()
	{
		return urlFileName;
	}

	public void setUrlFileName(String urlFileName)
	{
		this.urlFileName = urlFileName;
	}

	public String getUrlContentType()
	{
		return urlContentType;
	}

	public void setUrlContentType(String urlContentType)
	{
		this.urlContentType = urlContentType;
	}

	/**
	 * Description : 增加数据
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String addDataByViewForm() throws Exception
	{
		// 上传的图片类型
		String flieType = urlFileName.substring(urlFileName.indexOf(".") + 1);
		if (ViewUtil.isNuLLStr(formPanelId))
		{
			formPanelId = request.getParameter("formPanelId");
		}
		Map inputMap = ViewAction.requestParameterMap2Map(request);

		if (url != null)
		{
			if (url.length() == 0 || url.length() > maxSize)
			{
				Map result = new HashMap();
				result.put("failure", true);
				result.put("msg", "icon图片文件大小必须大于0KB且小于"
						+ Math.round((maxSize / 1000)) + "KB!");
				write(JSONObject.fromObject(result).toString());
				return null;
			} else if (iconType.indexOf(flieType) == -1)
			{
				Map result = new HashMap();
				result.put("failure", true);
				result.put("msg", "上传的图片格式不正确");
				write(JSONObject.fromObject(result).toString());
				return null;
			} else
			{
				StringBuilder fileName = new StringBuilder();
				fileName.append(new SimpleDateFormat("yyyyMMddHHmmssSSSS")
						.format(new Date()));
				fileName.append(AdamsConstant.ICON_SUF).append(
						urlFileName.substring(urlFileName.indexOf(".")));

				FileUtil.createPhotoPath(iconDir);
				File targetFile = new File(iconDir + File.separator + fileName);
				FileUtil.copy(url, targetFile);
				// article.setTiltePhotoName(urlFileName);
				// article.setTiltePhoto(titlePhotoUrl + AdamsConstant.SPT
				// + fileName.toString());
				String tempUrl = iconUrl + AdamsConstant.SPT + fileName;
				inputMap.put("url", tempUrl);
			}
		}

		Map map = viewParseService.saveOrUpdateDataInViewForm(getUserId(),
				menuId, formPanelId, inputMap);
		write(JSONObject.fromObject(map).toString());
		return null;
	}

	/**
	 * Description : 更新数据
	 * 
	 * @return
	 * @throws Exception
	 * 
	 */
	@SuppressWarnings("unchecked")
	public String updateDataByViewForm() throws Exception
	{
		if (ViewUtil.isNuLLStr(formPanelId))
		{
			formPanelId = request.getParameter("formPanelId");
		}
		Map inputMap = ViewAction.requestParameterMap2Map(request);

		if (url != null)
		{
			// 上传的图片类型
			String flieType = urlFileName
					.substring(urlFileName.indexOf(".") + 1);

			if (url.length() == 0 || url.length() > maxSize)
			{
				Map result = new HashMap();
				result.put("failure", true);
				result.put("msg", "icon图片文件大小必须大于0KB且小于"
						+ Math.round((maxSize / 1000)) + "200KB!");
				write(JSONObject.fromObject(result).toString());
				return null;
			} else if (iconType.indexOf(flieType) == -1)
			{
				Map result = new HashMap();
				result.put("failure", true);
				result.put("msg", "上传的图片格式不正确");
				write(JSONObject.fromObject(result).toString());
			} else
			{

				StringBuilder fileName = new StringBuilder();
				fileName.append(new SimpleDateFormat("yyyyMMddHHmmssSSSS")
						.format(new Date()));
				fileName.append(AdamsConstant.ICON_SUF).append(
						urlFileName.substring(urlFileName.indexOf(".")));

				FileUtil.createPhotoPath(iconDir);
				File targetFile = new File(iconDir + File.separator + fileName);
				FileUtil.copy(url, targetFile);

				String tempUrl = iconUrl + AdamsConstant.SPT + fileName;
				inputMap.put("url", tempUrl);
			}
		}

		Map map = viewParseService.saveOrUpdateDataInViewForm(getUserId(),
				menuId, formPanelId, inputMap);
		write(JSONObject.fromObject(map).toString());
		return null;
	}
}
